package com.mdinic.blogovi.front.common.menu;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.cookies.CookieUtils;

import com.mdinic.blogovi.front.common.AjaxLinkPerson;
import com.mdinic.blogovi.front.common.SmallPersonPhoto;
import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.front.page.HomePage;
import com.mdinic.blogovi.front.page.addblogpost.AddBlogPostPage;
import com.mdinic.blogovi.front.page.blogpost.BlogPostListPage;
import com.mdinic.blogovi.front.page.faved.BlogPostFavsPage;
import com.mdinic.blogovi.front.page.person.PersonViewPage;
import com.mdinic.blogovi.front.page.shared.BlogPostSharedPage;
import com.mdinic.blogovi.front.page.waitforapproval.BlogPostListNotApprovedPage;
import com.mdinic.blogovi.service.PersonService;
import com.mdinic.blogovi.service.Vokativ;

public class TopMenuPerson extends Panel {

    private static final String PROFILE_LINK = "profile_link";
    private static final String BLOG_POST_LINK = "blog_post_list_link";
    private static final String BLOG_POST_NOT_APPROVED_LINK = "blog_post_list_not_approved_link";
    private static final String FAVS_LINK = "favs_link";
    private static final String SHARED_LINK = "shared_link";

    private static final String ID = "top_menu";

    private static final String AUTH_CODE_COOKIE = "bauth";

    private static final long serialVersionUID = 1L;

    @SpringBean
    private PersonService personService;

    @SpringBean
    private Vokativ vokativ;

    public TopMenuPerson() {
        super(ID);
        setOutputMarkupId(true);

        add(new AjaxLinkPerson("add_link_link", false) {

            private static final long serialVersionUID = 1L;

            @Override
            public void click(AjaxRequestTarget target) {
                setResponsePage(AddBlogPostPage.class);
            }

        });

        PageParameters userParameters = new PageParameters();
        userParameters.add(PersonViewPage.URL_PARAM_NAME, BSession.get().getPerson().getId());
        BookmarkablePageLink<PersonViewPage> personLink = new BookmarkablePageLink<PersonViewPage>(PROFILE_LINK,
                PersonViewPage.class, userParameters);

        BookmarkablePageLink<BlogPostListPage> blogPostListLink = new BookmarkablePageLink<BlogPostListPage>(
                BLOG_POST_LINK, BlogPostListPage.class);

        BookmarkablePageLink<BlogPostListNotApprovedPage> blogPostListNotApprovedLink = new BookmarkablePageLink<BlogPostListNotApprovedPage>(
                BLOG_POST_NOT_APPROVED_LINK, BlogPostListNotApprovedPage.class);
        BookmarkablePageLink<BlogPostFavsPage> favsLink = new BookmarkablePageLink<BlogPostFavsPage>(FAVS_LINK,
                BlogPostFavsPage.class);

        BookmarkablePageLink<BlogPostSharedPage> sharedLink = new BookmarkablePageLink<BlogPostSharedPage>(SHARED_LINK,
                BlogPostSharedPage.class);

        personLink.add(new Label("profile_name", vokativ.makeVokativ(BSession.get().getPerson().getFirstname())));
        add(blogPostListNotApprovedLink);
        add(personLink);
        add(blogPostListLink);
        add(favsLink);
        add(sharedLink);

        add(new Link<Void>("logout_link") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                CookieUtils cu = new CookieUtils();
                cu.remove(AUTH_CODE_COOKIE);
                personService.logout(BSession.get().getPerson());
                BSession.get().setPerson(null);
                setResponsePage(HomePage.class);
            }
        });

        add(new SmallPersonPhoto("avatar", BSession.get().getPerson()));

    }
}
